package cn.me.sso.config;
import cn.me.sso.service.SsoInMemoryAuthorizationCodeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;
import java.util.Arrays;
/**
 * 授权服务配置
 */
@Configuration
@EnableAuthorizationServer
@Order(0)
public class SsoServerConfig extends AuthorizationServerConfigurerAdapter {
  @Autowired
  private DataSource dataSource;
  @Autowired
  private AuthenticationManager authenticationManager;
  @Autowired
  private SsoInMemoryAuthorizationCodeServices authorizationCodeServices;
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.withClientDetails(clientDetails());
  }
  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) {
    //配置这个解决无法使用post获取access token的问题
    security.allowFormAuthenticationForClients();
    security.checkTokenAccess("permitAll()");
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter(), tokenEnhancer()));
    endpoints.tokenStore(tokenStore())
      // .accessTokenConverter(jwtAccessTokenConverter())
      .tokenEnhancer(tokenEnhancerChain);
    endpoints.authenticationManager(authenticationManager)
      //
      .authorizationCodeServices(authorizationCodeServices);
  }
  /**
   * 声明clientDetail的实现
   *
   * @return JdbcClientDetailsService
   */
  @Bean
  public ClientDetailsService clientDetails() {
    return new JdbcClientDetailsService(dataSource);
  }
  /**
   * 1.令牌生成器 对称加密
   *
   * @return JwtAccessTokenConverter
   */
  @Bean
  public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    jwtAccessTokenConverter.setSigningKey("secret");
    return jwtAccessTokenConverter;
  }
  /**
   * token 实现
   *
   * @return TokenStore
   */
  @Bean
  public TokenStore tokenStore() {
    return new JwtTokenStore(jwtAccessTokenConverter());
  }
  /**
   * 自定义扩展令牌
   *
   * @return TokenEnhancer
   */
  @Bean
  public TokenEnhancer tokenEnhancer() {
    return new SsoTokenEnhancer();
  }
}
